﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for PickAvailableKPIUnSignHandler
    /// </summary>
    public class PickAvailableKPIUnSignHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["KPI"]);
            string action = HttpContext.Current.Request.QueryString["action"];
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["User"]));

            //KPIFacade df = new KPIFacade();
            //KPI KPIs = df.GetKPIByID(ID);

            //KPIAssign Existing = new KPIAssignFacade().GetAllKPIAssignsByKPIID(ID).Where(x => x.CreateBy == CurrUser.Email && x.isActive &&
            //    x.SubBranch.ID == CurrUser.SubBranch.ID && x.Verifier.ID == CurrUser.ID && x.Weight == KPIs.Weight).FirstOrDefault();

            ////KPIAssign Existing = new KPIAssignFacade().GetAllKPIAssignsByKPIID(ID).Where(x => x.isActive &&
            ////  x.SubBranch.ID == CurrUser.SubBranch.ID && x.Weight == KPIs.Weight).FirstOrDefault();


            switch (action)
            {
                case "pick":
                   
                    //KPIAssign ka = new KPIAssign()
                    //{
                    //    ID = 0,
                    //    isActive = true,
                    //    KPI = KPIs,
                    //    Location = CurrUser.SubBranch.Location,
                    //    ManualCalculation = KPIs.ManualCalculation,
                    //    Polarization = KPIs.Polarization,
                    //    RGM = CurrUser.SubBranch.Location.RGM,
                    //    SubBranch = CurrUser.SubBranch,
                    //    UoM = KPIs.UoM,
                    //    UoMDetailMax = KPIs.UoMDetailMax,
                    //    UoMDetailMin = KPIs.UoMDetailMin,
                    //    UoMEnableFlag = KPIs.UoMEnableFlag,
                    //    Verifier = CurrUser,
                    //    Weight = KPIs.Weight,
                    //    WeightEnableFlag = KPIs.WeightEnableFlag,
                    //    CreateBy = CurrUser.Email
                    //};

                    msg = new KPIAssignDetailFacade().SignKPI(ID);

                            
                    break;

                
                default: break;
            }

            return msg;
        }
    }
}